import 'package:clean_app/asset/storage.dart';
import 'package:clean_app/interface/mid_interface.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

isLogined(BuildContext context, {bool listen = true}) {
  return Provider.of<UserProvider>(context, listen: listen).isLogined;
}

class UserProvider extends ChangeNotifier {
  bool isLogined = false;

  Map info = {};

  pullInfo() async {
    var res = await AuthApi().appUsersMe();
    print(res["data"]);
    try {
      info = res["data"];
      refresh();
    } catch (_) {}
  }

  getLogined() async {
    var tmp = await getStorage(key: "token", initData: "");
    if (tmp != "") {
      isLogined = true;
      pullInfo();
    } else {
      isLogined = false;
    }
    notifyListeners();
  }

  logout() async {
    await setStorage(key: "token", value: "");
    isLogined = false;
    notifyListeners();
  }

  refresh() {
    notifyListeners();
  }
}
